<?php
namespace app\common\controller;
use think\Loader;
use think\Controller;
use think\Db;
use app\common\model\CorePayLog;

/**
* 后台公用基础控制器
* @author taigu
* @version 2017-9-22
*/
class PaylogBaseController extends Controller {
	
	//打印付款日志
	function print_log($out_trade_no){
		//预付款日志
		$paylog=db("log_paylog")->where('order_sn', $out_trade_no)->find();
		//付款用户
		$user_info = db('info_user')->where('id',$paylog['uid'])->find();
		//收款商家
		$agent_info = db('info_agent')->where('id',$paylog['agent_id'])->find();
		//支付类型
		$order_type = $paylog['order_type'];
		
		if($order_type == 1){	//订单
			
		}else if($order_type == 2 ){	//扫码支付
		// 启动事务
		Db::startTrans();
		try{
			//实付金额
			$price	 = $paylog['balance'];	
			//应付金额				
			$pay_money =	$paylog['origin_price'];	
			//付款金额1才可以赠送鼓励金
			$bijiao = bccomp(floatval($pay_money), floatval(1), 2);	
			//开启 鼓励金营销
			if($agent_info['is_score'] == 1 && $bijiao>=0){
				//全额付款(需送鼓励金)
				if(($price * 100) == ($pay_money * 100)){
					//判断商家是否赠送鼓励金
					if($agent_info['zs_score']>0){
						//商家送鼓励金比例
						$give_scale = bcdiv($agent_info['zs_score'], 100, 4);
						//计算送出鼓励金 = 付款金额 * 商家送鼓励金比例
						$give_score = bcmul($price, $give_scale, 4);
						//增加用户鼓励金
						db('info_user')->where('id', $user_info['id'])->setInc('score', $give_score);
						//用户鼓励金记录
						$this->user_score_log($paylog,5,$give_score,1,"扫码消费".$price."元，送鼓励金".$give_score);
						//减去商家鼓励金
						db('info_agent')->where('id', $agent_info['id'])->setDec('score', $give_score);
						//商家鼓励金记录
						$this->agent_score_log($paylog,3,$give_score,2,'用户'.$user_info['mobile'].'消费赠送鼓励金'.$give_score);
						//更新商家，用户鼓励金变化情况(便于统计)
						db("log_paylog")
						->where('order_sn', $out_trade_no)
						->update(['score'=>$give_score,'member_score' =>$give_score,'agent_score'=>-$give_score,'payment_type'=>1]);
						/***************************提前分佣start********************************************/
						//鼓励金费率
						//$score_fee = $distibution['store_jf_fee']/100;
						$score_fee = 0.05;
						//计算总鼓励金手续费
						$procedure_fee = bcmul($give_score, $score_fee, 4);
						//手续费大于0.1元，才进行分佣
						if(bccomp(floatval($procedure_fee), floatval(0.1), 4) >= 0){
							//计算省代鼓励金佣金
							$score_pt_fee = bcmul($give_score, 0.01, 4);
							//手续费大于0.01元，才收取手续费
							if($agent_info['agency_id1'] && $agent_info['agency_id1']>0 && bccomp(floatval($score_pt_fee),floatval(0.01), 4) >=0){
								//将鼓励金转为佣金
								$province_commission = bcmul($score_pt_fee, 0.1, 4);
								//增加省代佣金
								db('info_oem')->where('id', $agent_info['agency_id1'])->setInc('commission', $province_commission);
								//更新省代佣金变化情况(便于统计)
								db("log_paylog")->where('order_sn', $out_trade_no)->update(['agency_id1_commission' =>$province_commission]);
								//更新省代佣金日志
								$this->oem_commission_log($paylog,2,$province_commission,1,2,"商家".$agent_info['mobile']."鼓励金佣金");
								//剩余佣金
								$procedure_fee = bcsub($procedure_fee, $score_pt_fee, 4);
							}
							
							//计算市代鼓励金佣金
							//$score_up_fee1 = bcmul($give_score, $distibution['store_upper1_fee']/100, 2); 
							$score_up_fee1 = bcmul($give_score, 0.02, 4);
							//手续费大于0.1元，才收取手续费
							if($agent_info['agency_id2'] && $agent_info['agency_id2']>0 && bccomp(floatval($score_up_fee1),floatval(0.01), 4) >= 0){
								//将鼓励金转为佣金
								$city_commission = bcmul($score_up_fee1, 0.1, 4);
								//增加市代佣金
								db('info_oem')->where('id', $agent_info['agency_id2'])->setInc('commission', $city_commission);
								//更新市代佣金变化情况(便于统计)
								db("log_paylog")->where('order_sn', $out_trade_no)->update(['agency_id2_commission' =>$city_commission]);
								//更新市代佣金日志
								$this->oem_commission_log($paylog,2,$city_commission,1,3,"商家".$agent_info['mobile']."鼓励金佣金");
								//剩余佣金
								$procedure_fee = bcsub($procedure_fee, $score_up_fee1, 4);
							}
							
							//计算用户上级鼓励金佣金
							$score_up_fee2 = bcmul($give_score,0.01, 2);
							//手续费大于0.1元，才收取手续费
							if(bccomp(floatval($score_up_fee2),floatval(0.01), 4) >= 0){
								//增加用户上级鼓励金佣金
								db('info_user')->where('id', $user_info['level'])->setInc('score', $score_up_fee2);
								//更新用户上级鼓励金佣金日志
								$this->user_score_log($paylog,2,$score_up_fee2,1,"下属商家".$agent_info['mobile']."鼓励金分佣");
								//剩余鼓励金佣金
								$procedure_fee = bcsub($procedure_fee, $score_up_fee2, 4);
							}
							//OEM鼓励金佣金
							if(bccomp(floatval($procedure_fee),floatval(0.01), 4) >= 0){
								//将鼓励金转为佣金
								$oem_commission = bcmul($procedure_fee, 0.1, 4);
								//增加OEM佣金
								db('info_oem')->where('id', $agent_info['oem_id'])->setInc('commission', $oem_commission);
								//更新OEM佣金变化情况(便于统计)
								db("log_paylog")->where('order_sn', $out_trade_no)->update(['oem_commission' =>$oem_commission]);
								//更新OEM佣金日志
								$this->oem_commission_log($paylog,2,$oem_commission,1,1,"商家".$agent_info['mobile']."鼓励金佣金");
							}
							$cut_procedure_fee = bcmul($give_score, $score_fee, 4);
							//商家提前分佣日志
							$this->ahead_agent_score_log($paylog,$cut_procedure_fee,"赠送鼓励金提前分佣");
						}
						/***************************提现分佣end********************************************/
					}
				}else{
					//判断商家是否抵扣鼓励金
					if($agent_info['ad_score']>0){
						//商家抵扣鼓励金比例
						$counteract = bcdiv($agent_info['ad_score'], 100, 4);
						//计算抵扣鼓励金 = 应付金额 * 商家抵扣比例
						$score_change = bcmul($paylog['origin_price'] , $counteract, 4);
						//更新商家，用户鼓励金变化情况(便于统计)
						db("log_paylog")
						->where('order_sn', $out_trade_no)
						->update(['score'=>$score_change,'member_score' =>-$score_change,'agent_score'=>$score_change,'payment_type'=>2]);
						//$user_info $agent_info
						//减用户鼓励金
						db('info_user')->where('id', $user_info['id'])->setDec('score', $score_change);
						//用户鼓励金记录
						$this->user_score_log($paylog,5,$score_change,2,"在商家".$agent_info['mobile']."消费,抵扣鼓励金:".$score_change);
						//加商家鼓励金
						db('info_agent')->where('id', $agent_info['id'])->setInc('score', $score_change);
						//商家鼓励金记录
						$this->agent_score_log($paylog,3,$score_change,1,"收取用户".$user_info['mobile']."鼓励金抵扣:".$score_change);
						//鼓励金费率
						//$score_fee = $distibution['store_jf_fee']/100;
						$score_fee = 0.05;
						//计算鼓励金手续费
						$poundage = bcmul($score_change, $score_fee, 4);
						if(bccomp(floatval($poundage), floatval(0.1), 2) >= 0){
							//减去商家鼓励金手续费
							db('info_agent')->where('id', $agent_info['id'])->setDec('score', $poundage);
							//减去商家鼓励金手续费日志
							$this->agent_score_log($paylog,$order_type,$poundage,2,"鼓励金抵扣手续费:".$poundage);
						}
					}
				}
			}
			//新增用户金额变化日志
			$this->user_money_log($paylog,5,$paylog['balance'],2,"向商家二维码付款:".$paylog['balance']);
			//增加商家余额
			//db('info_agent')->where('id', $agent_info['id'])->setInc('balance', $paylog['balance']);
			//商家余额变化日志 
			$this->agent_money_log($paylog,3,$paylog['balance'],1,"用户扫码付款:".$paylog['balance']);
			//最低费率
			$rate = 0.0025;
			//商家费率
			$agent_rate = bcdiv($agent_info['cb_huilu'], 100, 4);
			//市代费率佣金 = 商家费率-最低费率
			$city_rate = bcsub($agent_rate, $rate, 4);
			//市代费率佣金>=0.0001 给市代分佣
			if($agent_info['agency_id2'] && $agent_info['agency_id2']>0 && bccomp($city_rate,0.0001, 4)>=0){
				//计算市代佣金 = 商家收款金额 * 市代费率佣金
				$city_rate_commission = bcmul($paylog['balance'], $city_rate, 4);
				//新增市代佣金
				db('info_oem')->where('id', $agent_info['agency_id2'])->setInc('commission', $city_rate_commission);
				//市代佣金记录
				$this->oem_commission_log($paylog,7,$city_rate_commission,1,3,"商家".$agent_info['mobile']."费率佣金");
			}

		/********************************广告费分佣start***************************************/
			//判断付款用户是否是自己的线下会员
			$cut_shou_user = db('info_user')->where('id',$agent_info['user_id'])->find();
			//开启云商圈
			if($user_info['level'] != $cut_shou_user['id']){
				//单客次广告费
				$ad_one_money = 0.2;
				//联盟主广告佣金比例
				$union_main_scale = 0.2;
				//付款用户上级广告佣金比例
				$user_up_scale = 0.3;
				//计算联盟主广告佣金20%,
				$union_main_adsmoney = bcmul($ad_one_money, $union_main_scale, 2);
				//计算付款用户上级广告佣金30%
				$user_up_adsmoney = bcmul($ad_one_money, $user_up_scale, 2);
				//查询商家已加入联盟 union_member
    			$where['b.is_state'] = 1;	//联盟为正常状态
    			$where['a.agent_id'] = $agent_info['id'];
    			$where['a.state'] = 1;
    	 
		    	$list = db('union_member')
						->alias('a')
						->field('a.u_id as union_user_id ')
						->join('union_agent b','a.union_id = b.id')
						->where($where)
						->order('a.add_time desc')
						->select();
						
				//检查商家是否是联盟成员
				if(count($list)>0){
					//计算商家本次需支付联盟主广告费
					$union_main_all_adsmoney = bcmul(count($list), $union_main_adsmoney, 2);
					//计算商家本次一共需支付广告费
					$total_adsmoney = bcadd($union_main_all_adsmoney, $user_up_adsmoney, 2);
					//当前店铺广告费余额大于本次广告费，广告费大于等于0.1元
					if(bccomp($agent_info['ad_money'], $total_adsmoney, 2)>=0){
						//$user_info $agent_info $cut_shou_user
						//减去商家广告费
						db('info_agent')->where('id', $agent_info['id'])->setDec('ad_money', $total_adsmoney);
						//商家广告费日志 
						$this->agent_money_log($paylog,8,$total_adsmoney,2,"支付广告费");
						//增加付款用户上级佣金
						db('info_user')->where('id', $user_info['level'])->setInc('commission', $user_up_adsmoney);
						//用户上级分佣日志
						$this->user_commission_log($paylog,3,$user_up_adsmoney,1,"收取推荐会员:".$user_info['mobile']."广告佣金");
						//检查用户是否加入联盟
						if(count($list)>0){
							//向联盟主支付广告费
							for($i=0;$i<count($list);$i++){
								//增加付款用户上级佣金
								db('info_user')->where('id', $list[$i]['union_user_id'])->setInc('commission', $union_main_adsmoney);
								//用户上级分佣日志
								$this->user_commission_log($paylog,3,$union_main_adsmoney,1,"联盟成员:".$user_info['mobile']."广告佣金");
							}
						}else{
							//增加OEM广告费佣金
							db('info_oem')->where('id', $agent_info['oem_id'])->setInc('commission', $union_main_adsmoney);
							//更新OEM佣金日志
							$this->oem_commission_log($paylog,3,$union_main_adsmoney,1,1,"商家".$agent_info['mobile']."鼓励金佣金");
						}
					}else{
						//商家广告费不足，关闭商家云商圈
						db('info_agent')->where('id', $agent_info['id'])->update(['is_union' => 2]);
					}
				}
			}

		/********************************广告费分佣end***************************************/
			// 获取短信模板
			$template = db('sms_template') -> find(4);
			$msg['name'] = $price;
			//检查是否分店
			if($paylog['chain_id']>0){
				//获取收银员信息
				$employee_info = db('agent_clerk')->where('id',$paylog['clerk_id'])->find();
				//发送短信，提醒分店负责人
				//$msg = send_sms($employee_info['mobile'], $template['tid'], json_encode($msg), $template['sign_name']);
			}else{
				//发送短信，提醒商家
				//$msg = send_sms($agent_info['tel'], $template['tid'], json_encode($msg), $template['sign_name']);
			}
			//提交
			Db::commit();
			//修改订单状态
			$this->add_corepaylog($paylog);
		}catch ( Exception $e ) {
		    dump($e->getMessage());
		    //回滚
		    Db::rollback();
		    exit;
		}
		}else if($order_type == 3){	//用户充值
			
		}else if($order_type == 4){	//广告充值
			// 启动事务
			Db::startTrans();
			try{
			    //更新用户交易记录 
				$this->user_money_log($paylog,3,$paylog['balance'],2,"为商家".$agent_info['agent_name']."充值广告费");
				//增加店铺广告余额
				db('info_agent')->where('id', $agent_info['id'])->setInc('ad_money', $paylog['balance']);
				//商家资金变化日志 
				$this->agent_money_log($paylog,7,$paylog['balance'],1,'广告费充值');
				//计算提前分佣金额
				$total_yongj = bcmul($paylog['balance'], 0.5, 4); 
				
				//省代佣金
				if($agent_info['agency_id1'] && $agent_info['agency_id1']>0){
					//计算省代佣金
					$province_yongj = bcmul($paylog['balance'], 0.1, 4); 
					//新增省代佣金
					db('info_oem')->where('id', $agent_info['agency_id1'])->setInc('commission', $province_yongj);
					//省代佣金记录
					$this->oem_commission_log($paylog,3,$province_yongj,1,2,'商家'.$agent_info['mobile'].'广告充值佣金');
					//计算剩余佣金
					$total_yongj = bcsub($total_yongj, $province_yongj, 4);
				}
				//市代佣金
				if($agent_info['agency_id2'] && $agent_info['agency_id2']>0){
					//计算省代佣金
					$city_yongj = bcmul($paylog['balance'], 0.3, 4); 
					//新增市代佣金
					db('info_oem')->where('id', $agent_info['agency_id2'])->setInc('commission', $city_yongj);
					//市代佣金记录
					$this->oem_commission_log($paylog,3,$city_yongj,1,3,'商家'.$agent_info['mobile'].'广告充值佣金');
					//计算剩余佣金
					$total_yongj = bcsub($total_yongj, $city_yongj, 4);
				}
				//新增oem佣金
				db('info_oem')->where('id', $agent_info['oem_id'])->setInc('commission', $total_yongj);
				//oem佣金记录
				$this->oem_commission_log($paylog,3,$total_yongj,1,1,'商家'.$agent_info['mobile'].'广告充值佣金');
			    // 提交事务
			    Db::commit();    
			    //修改订单状态
				$this->add_corepaylog($paylog);
			    //支付宝付款跳转
				if($paylog['pay_type']==3){//支付宝回调页面
					$redirect_url=config('API_DOMAIN')."agent/index/index.html";
					Header("Location: $redirect_url");
					exit;
				}
			    
			} catch (\Exception $e) {
				// 回滚事务
			    Db::rollback();
				//print $e->getMessage();
			    $this->error('数据异常请联系管理员订单号：'.$paylog['order_sn']);
			    
			}
		}else if($order_type == 5){	//鼓励金充值
			//鼓励金变化值
			$score_charge = bcmul($paylog['balance'], 10, 2);
			//增加商家鼓励金
			db('info_agent')->where('id', $agent_info['id'])->setInc('score', $score_charge);
			//商家鼓励金记录
			$this->agent_score_log($paylog,5,$score_charge,1,"商家鼓励金充值");
			//修改订单状态
			$this->add_corepaylog($paylog);
			//跳转
			if($paylog['pay_type']==3){//支付宝回调页面
				$redirect_url=config('API_DOMAIN')."agent/index/index.html";
				Header("Location: $redirect_url");
				exit;
			}
		}else if($order_type == 6){
			// 启动事务
			Db::startTrans();
			try{
				//实付金额
				$price	 = $paylog['balance'];	
				//应付金额				
				$pay_money =	$paylog['origin_price'];	
				//付款金额1才可以赠送鼓励金
				$bijiao = bccomp(floatval($pay_money), floatval(1), 2);	
				//获取订单信息
				$sy_order_info = db('agent_syorder')->where('sn',$paylog['order_sn'])->find();
				
				//开启 鼓励金营销
				if($agent_info['is_score'] == 1 && $bijiao>=0){
					//全额付款(需送鼓励金)
					if(($price * 100) == ($pay_money * 100)){
						//判断该订单是否赠送鼓励金
						if(bccomp($sy_order_info['give_score'], 0.00, 2)>=0){
							//赠送鼓励金金额
							$give_score = $sy_order_info['give_score'];
							//增加用户鼓励金
							db('info_user')->where('id', $user_info['id'])->setInc('score', $give_score);
							//用户鼓励金记录
							$this->user_score_log($paylog,5,$give_score,1,"扫码消费".$price."元，送鼓励金".$give_score);
							//减去商家鼓励金
							db('info_agent')->where('id', $agent_info['id'])->setDec('score', $give_score);
							//商家鼓励金记录
							$this->agent_score_log($paylog,3,$give_score,2,'用户'.$user_info['mobile'].'消费赠送鼓励金'.$give_score);
							//更新商家，用户鼓励金变化情况(便于统计)
							db("log_paylog")
							->where('order_sn', $out_trade_no)
							->update(['score'=>$give_score,'member_score' =>$give_score,'agent_score'=>-$give_score,'payment_type'=>1]);
							/***************************提前分佣start********************************************/
							//鼓励金费率
							//$score_fee = $distibution['store_jf_fee']/100;
							$score_fee = 0.05;
							//计算总鼓励金手续费
							$procedure_fee = bcmul($give_score, $score_fee, 4);
							//手续费大于0.1元，才进行分佣
							if(bccomp(floatval($procedure_fee), floatval(0.1), 4) >= 0){
								//计算省代鼓励金佣金
								$score_pt_fee = bcmul($give_score, 0.01, 4);
								//手续费大于0.01元，才收取手续费
								if($agent_info['agency_id1'] && $agent_info['agency_id1']>0 && bccomp(floatval($score_pt_fee),floatval(0.01), 4) >=0){
									//将鼓励金转为佣金
									$province_commission = bcmul($score_pt_fee, 0.1, 4);
									//增加省代佣金
									db('info_oem')->where('id', $agent_info['agency_id1'])->setInc('commission', $province_commission);
									//更新省代佣金变化情况(便于统计)
									db("log_paylog")->where('order_sn', $out_trade_no)->update(['agency_id1_commission' =>$province_commission]);
									//更新省代佣金日志
									$this->oem_commission_log($paylog,2,$province_commission,1,2,"商家".$agent_info['mobile']."鼓励金佣金");
									//剩余佣金
									$procedure_fee = bcsub($procedure_fee, $score_pt_fee, 4);
								}
								
								//计算市代鼓励金佣金
								//$score_up_fee1 = bcmul($give_score, $distibution['store_upper1_fee']/100, 2); 
								$score_up_fee1 = bcmul($give_score, 0.02, 4);
								//手续费大于0.1元，才收取手续费
								if($agent_info['agency_id2'] && $agent_info['agency_id2']>0 && bccomp(floatval($score_up_fee1),floatval(0.01), 4) >= 0){
									//将鼓励金转为佣金
									$city_commission = bcmul($score_up_fee1, 0.1, 4);
									//增加市代佣金
									db('info_oem')->where('id', $agent_info['agency_id2'])->setInc('commission', $city_commission);
									//更新市代佣金变化情况(便于统计)
									db("log_paylog")->where('order_sn', $out_trade_no)->update(['agency_id2_commission' =>$city_commission]);
									//更新市代佣金日志
									$this->oem_commission_log($paylog,2,$city_commission,1,3,"商家".$agent_info['mobile']."鼓励金佣金");
									//剩余佣金
									$procedure_fee = bcsub($procedure_fee, $score_up_fee1, 4);
								}
								
								//计算用户上级鼓励金佣金
								$score_up_fee2 = bcmul($give_score,0.01, 2);
								//手续费大于0.1元，才收取手续费
								if(bccomp(floatval($score_up_fee2),floatval(0.01), 4) >= 0){
									//增加用户上级鼓励金佣金
									db('info_user')->where('id', $user_info['level'])->setInc('score', $score_up_fee2);
									//更新用户上级鼓励金佣金日志
									$this->user_score_log($paylog,2,$score_up_fee2,1,"下属商家".$agent_info['mobile']."鼓励金分佣");
									//剩余鼓励金佣金
									$procedure_fee = bcsub($procedure_fee, $score_up_fee2, 4);
								}
								//OEM鼓励金佣金
								if(bccomp(floatval($procedure_fee),floatval(0.01), 4) >= 0){
									//将鼓励金转为佣金
									$oem_commission = bcmul($procedure_fee, 0.1, 4);
									//增加OEM佣金
									db('info_oem')->where('id', $agent_info['oem_id'])->setInc('commission', $oem_commission);
									//更新OEM佣金变化情况(便于统计)
									db("log_paylog")->where('order_sn', $out_trade_no)->update(['oem_commission' =>$oem_commission]);
									//更新OEM佣金日志
									$this->oem_commission_log($paylog,2,$oem_commission,1,1,"商家".$agent_info['mobile']."鼓励金佣金");
								}
								$cut_procedure_fee = bcmul($give_score, $score_fee, 4);
								//商家提前分佣日志
								$this->ahead_agent_score_log($paylog,$cut_procedure_fee,"赠送鼓励金提前分佣");
							}
							/***************************提现分佣end********************************************/
						}
					}else{
						//判断当前订单是否抵扣鼓励金
						if(bccomp($sy_order_info['need_score'], 0.00, 2)>=0){
							//抵扣鼓励金金额
							$score_change = $sy_order_info['need_score'];
							//更新商家，用户鼓励金变化情况(便于统计)
							db("log_paylog")
							->where('order_sn', $out_trade_no)
							->update(['score'=>$score_change,'member_score' =>-$score_change,'agent_score'=>$score_change,'payment_type'=>2]);
							//减用户鼓励金
							db('info_user')->where('id', $user_info['id'])->setDec('score', $score_change);
							//用户鼓励金记录
							$this->user_score_log($paylog,5,$score_change,2,"在商家".$agent_info['mobile']."消费,抵扣鼓励金:".$score_change);
							//加商家鼓励金
							db('info_agent')->where('id', $agent_info['id'])->setInc('score', $score_change);
							//商家鼓励金记录
							$this->agent_score_log($paylog,3,$score_change,1,"收取用户".$user_info['mobile']."鼓励金抵扣:".$score_change);
							//鼓励金费率
							//$score_fee = $distibution['store_jf_fee']/100;
							$score_fee = 0.05;
							//计算鼓励金手续费
							$poundage = bcmul($score_change, $score_fee, 4);
							if(bccomp(floatval($poundage), floatval(0.1), 2) >= 0){
								//减去商家鼓励金手续费
								db('info_agent')->where('id', $agent_info['id'])->setDec('score', $poundage);
								//减去商家鼓励金手续费日志
								$this->agent_score_log($paylog,$order_type,$poundage,2,"鼓励金抵扣手续费:".$poundage);
							}
						}
					}
				}
				//新增用户金额变化日志
				$this->user_money_log($paylog,5,$paylog['balance'],2,"向商家二维码付款:".$paylog['balance']);
				//增加商家余额
				//db('info_agent')->where('id', $agent_info['id'])->setInc('balance', $paylog['balance']);
				//商家余额变化日志 
				$this->agent_money_log($paylog,3,$paylog['balance'],1,"用户扫码付款:".$paylog['balance']);
				//最低费率
				$rate = 0.0025;
				//商家费率
				$agent_rate = bcdiv($agent_info['cb_huilu'], 100, 4);
				//市代费率佣金 = 商家费率-最低费率
				$city_rate = bcsub($agent_rate, $rate, 4);
				//市代费率佣金>=0.0001 给市代分佣
				if($agent_info['agency_id2'] && $agent_info['agency_id2']>0 && bccomp($city_rate,0.0001, 4)>=0){
					//计算市代佣金 = 商家收款金额 * 市代费率佣金
					$city_rate_commission = bcmul($paylog['balance'], $city_rate, 4);
					//新增市代佣金
					db('info_oem')->where('id', $agent_info['agency_id2'])->setInc('commission', $city_rate_commission);
					//市代佣金记录
					$this->oem_commission_log($paylog,7,$city_rate_commission,1,3,"商家".$agent_info['mobile']."费率佣金");
				}
	
			/********************************广告费分佣start***************************************/
				//判断付款用户是否是自己的线下会员
				$cut_shou_user = db('info_user')->where('id',$agent_info['user_id'])->find();
				//开启云商圈
				if($user_info['level'] != $cut_shou_user['id']){
					//单客次广告费
					$ad_one_money = 0.2;
					//联盟主广告佣金比例
					$union_main_scale = 0.2;
					//付款用户上级广告佣金比例
					$user_up_scale = 0.3;
					//计算联盟主广告佣金20%,
					$union_main_adsmoney = bcmul($ad_one_money, $union_main_scale, 2);
					//计算付款用户上级广告佣金30%
					$user_up_adsmoney = bcmul($ad_one_money, $user_up_scale, 2);
					//查询商家已加入联盟 union_member
	    			$where['b.is_state'] = 1;	//联盟为正常状态
	    			$where['a.agent_id'] = $agent_info['id'];
	    			$where['a.state'] = 1;
	    	 
			    	$list = db('union_member')
							->alias('a')
							->field('a.u_id as union_user_id ')
							->join('union_agent b','a.union_id = b.id')
							->where($where)
							->order('a.add_time desc')
							->select();
							
					//检查商家是否是联盟成员
					if(count($list)>0){
						//计算商家本次需支付联盟主广告费
						$union_main_all_adsmoney = bcmul(count($list), $union_main_adsmoney, 2);
						//计算商家本次一共需支付广告费
						$total_adsmoney = bcadd($union_main_all_adsmoney, $user_up_adsmoney, 2);
						//当前店铺广告费余额大于本次广告费，广告费大于等于0.1元
						if(bccomp($agent_info['ad_money'], $total_adsmoney, 2)>=0){
							//$user_info $agent_info $cut_shou_user
							//减去商家广告费
							db('info_agent')->where('id', $agent_info['id'])->setDec('ad_money', $total_adsmoney);
							//商家广告费日志 
							$this->agent_money_log($paylog,8,$total_adsmoney,2,"支付广告费");
							//增加付款用户上级佣金
							db('info_user')->where('id', $user_info['level'])->setInc('commission', $user_up_adsmoney);
							//用户上级分佣日志
							$this->user_commission_log($paylog,3,$user_up_adsmoney,1,"收取推荐会员:".$user_info['mobile']."广告佣金");
							//检查用户是否加入联盟
							if(count($list)>0){
								//向联盟主支付广告费
								for($i=0;$i<count($list);$i++){
									//增加付款用户上级佣金
									db('info_user')->where('id', $list[$i]['union_user_id'])->setInc('commission', $union_main_adsmoney);
									//用户上级分佣日志
									$this->user_commission_log($paylog,3,$union_main_adsmoney,1,"联盟成员:".$user_info['mobile']."广告佣金");
								}
							}else{
								//增加OEM广告费佣金
								db('info_oem')->where('id', $agent_info['oem_id'])->setInc('commission', $union_main_adsmoney);
								//更新OEM佣金日志
								$this->oem_commission_log($paylog,3,$union_main_adsmoney,1,1,"商家".$agent_info['mobile']."鼓励金佣金");
							}
						}else{
							//商家广告费不足，关闭商家云商圈
							db('info_agent')->where('id', $agent_info['id'])->update(['is_union' => 2]);
						}
					}
				}
	
			/********************************广告费分佣end***************************************/
				// 获取短信模板
				$template = db('sms_template') -> find(4);
				$msg['name'] = $price;
				//检查是否分店
				if($paylog['chain_id']>0){
					//获取收银员信息
					$employee_info = db('agent_clerk')->where('id',$paylog['clerk_id'])->find();
					//发送短信，提醒分店负责人
					//$msg = send_sms($employee_info['mobile'], $template['tid'], json_encode($msg), $template['sign_name']);
				}else{
					//发送短信，提醒商家
					//$msg = send_sms($agent_info['tel'], $template['tid'], json_encode($msg), $template['sign_name']);
				}
				//获取订单信息
				$order_info = Db::table('agent_syorder')->where('sn',$out_trade_no)->find();
				//修改订单状态
				Db::table('agent_syorder')->where('sn',$out_trade_no)->update(['status' => 2]);
				//获取订单详情
				$order_item_info = Db::table('agent_syorder_items')->where('order_id',$order_info['id'])->select();
					
				foreach($order_item_info as $key=>$value){
					//获取当前商品信息
					$goods_info = Db::table('agent_goods')->where('id', $value['goods_id'])->find();
					//计算剩余库存
					$goods_stock = $goods_info['stock'] - $value['num'];
					//计算累计销量
					$goods_sales = $goods_info['sales'] + $value['num'];
					//判断是否抵扣
					if(($paylog['balance'] * 100) == ($paylog['origin_price'] * 100)){
						//累计销售金额
						$total_money 	= bcadd($goods_info['total_money'], bcmul($goods_info['original_price'], $value['num']), 2);
						//累计赠送积分
						$total_give_score = bcadd($goods_info['total_give_score'], bcmul($goods_info['zs_price'], $value['num']), 2);
						//累计抵扣积分	
						$total_need_score = $goods_info['total_need_score'];
					}else{
						//累计销售金额
						$total_money 	= bcadd($goods_info['total_money'], bcmul($goods_info['sale_price'], $value['num']), 2);
						//累计赠送积分
						$total_give_score = $goods_info['total_give_score'];
						//累计抵扣积分	
						$total_need_score = bcadd($goods_info['total_need_score'], bcmul($goods_info['dk_price'], $value['num']), 2);
					}
					
					//减库存
					Db::table('agent_goods')
					->where('id', $value['goods_id'])
					->update(['stock' => $goods_stock,
								'sales'=>$goods_sales,
								'total_money'=>$total_money,
								'total_give_score'=>$total_give_score,
								'total_need_score'=> $total_need_score
								]);
				}
				//提交
				Db::commit();
				//修改订单状态
				$this->add_corepaylog($paylog);
			}catch ( Exception $e ) {
			    dump($e->getMessage());
			    //回滚
			    Db::rollback();
			    exit;
			}
		}else if($order_type == 7){	//单商品
			// 启动事务
			Db::startTrans();
			try{
				//实付金额
				$price	 = $paylog['balance'];	
				//应付金额				
				$pay_money =	$paylog['origin_price'];	
				//付款金额1才可以赠送鼓励金
				$bijiao = bccomp(floatval($pay_money), floatval(1), 2);	
				//获取商品信息
				$goods_info = db('agent_goods')->where('id',$paylog['goods_id'])->find();
				
				//开启 鼓励金营销
				if($agent_info['is_score'] == 1 && $bijiao>=0){
					//全额付款(需送鼓励金)
					if(($price * 100) == ($pay_money * 100)){
						//判断商品是否赠送鼓励金
						if($goods_info['zs_price']>0){
							//计算赠送鼓励金 = 商品送鼓励金 * 商品数量
							$give_score = bcmul($goods_info['zs_price'], $paylog['goods_num'], 4);
							//增加用户鼓励金
							db('info_user')->where('id', $user_info['id'])->setInc('score', $give_score);
							//用户鼓励金记录
							$this->user_score_log($paylog,5,$give_score,1,"扫码消费".$price."元，送鼓励金".$give_score);
							//减去商家鼓励金
							db('info_agent')->where('id', $agent_info['id'])->setDec('score', $give_score);
							//商家鼓励金记录
							$this->agent_score_log($paylog,3,$give_score,2,'用户'.$user_info['mobile'].'消费赠送鼓励金'.$give_score);
							//更新商家，用户鼓励金变化情况(便于统计)
							db("log_paylog")
							->where('order_sn', $out_trade_no)
							->update(['score'=>$give_score,'member_score' =>$give_score,'agent_score'=>-$give_score,'payment_type'=>1]);
							/***************************提前分佣start********************************************/
							//鼓励金费率
							$score_fee = 0.05;
							//计算总鼓励金手续费
							$procedure_fee = bcmul($give_score, $score_fee, 4);
							//手续费大于0.1元，才进行分佣
							if(bccomp(floatval($procedure_fee), floatval(0.1), 4) >=100){
								//计算省代鼓励金佣金
								$score_pt_fee = bcmul($give_score, 0.01, 4);
								
								//手续费大于0.01元，才收取手续费
								if($agent_info['agency_id1'] && $agent_info['agency_id1']>0 &&bccomp(floatval($score_pt_fee),floatval(0.01), 4) >=0){
									//将鼓励金转为佣金
									$province_commission = bcmul($score_pt_fee, 0.1, 4);
									//增加省代佣金
									db('info_oem')->where('id', $agent_info['agency_id1'])->setInc('commission', $province_commission);
									//更新省代佣金变化情况(便于统计)
									db("log_paylog")->where('order_sn', $out_trade_no)->update(['agency_id1_commission' =>$province_commission]);
									//更新省代佣金日志
									$this->oem_commission_log($paylog,2,$province_commission,1,2,"商家".$agent_info['mobile']."鼓励金佣金");
									//剩余佣金
									$procedure_fee = bcsub($procedure_fee, $score_pt_fee, 4);
								}
								
								//计算市代鼓励金佣金
								//$score_up_fee1 = bcmul($give_score, $distibution['store_upper1_fee']/100, 2); 
								$score_up_fee1 = bcmul($give_score, 0.02, 4);
								//手续费大于0.1元，才收取手续费
								if($agent_info['agency_id2'] && $agent_info['agency_id2']>0 &&bccomp(floatval($score_up_fee1),floatval(0.01), 4) >= 0){
									//将鼓励金转为佣金
									$city_commission = bcmul($score_up_fee1, 0.1, 4);
									//增加市代佣金
									db('info_oem')->where('id', $agent_info['agency_id2'])->setInc('commission', $city_commission);
									//更新市代佣金变化情况(便于统计)
									db("log_paylog")->where('order_sn', $out_trade_no)->update(['agency_id2_commission' =>$city_commission]);
									//更新市代佣金日志
									$this->oem_commission_log($paylog,2,$city_commission,1,3,"商家".$agent_info['mobile']."鼓励金佣金");
									//剩余佣金
									$procedure_fee = bcsub($procedure_fee, $score_up_fee1, 4);
								}
								
								//计算用户上级鼓励金佣金
								$score_up_fee2 = bcmul($give_score,0.1, 2);
								//手续费大于0.1元，才收取手续费
								if(bccomp(floatval($score_up_fee2),floatval(0.01), 4) >= 0){
									//增加用户上级鼓励金佣金
									db('info_user')->where('id', $user_info['level'])->setInc('score', $score_up_fee2);
									//更新用户上级鼓励金佣金日志
									$this->user_score_log($paylog,2,$score_up_fee2,1,"下属商家".$agent_info['mobile']."鼓励金分佣");
									//剩余鼓励金佣金
									$procedure_fee = bcsub($procedure_fee, $score_up_fee1, 4);
								}
								
								//OEM鼓励金佣金
								if(bccomp(floatval($procedure_fee),floatval(0.01), 4) >= 0){
									//将鼓励金转为佣金
									$oem_commission = bcmul($procedure_fee, 0.1, 4);
									//增加OEM佣金
									db('info_oem')->where('id', $agent_info['oem_id'])->setInc('commission', $oem_commission);
									//更新OEM佣金变化情况(便于统计)
									db("log_paylog")->where('order_sn', $out_trade_no)->update(['oem_commission' =>$oem_commission]);
									//更新OEM佣金日志
									$this->oem_commission_log($paylog,2,$oem_commission,1,1,"商家".$agent_info['mobile']."鼓励金佣金");
								}
								$cut_procedure_fee = bcmul($give_score, $score_fee, 4);
								//商家提前分佣日志
								$this->ahead_agent_score_log($paylog,$cut_procedure_fee,"赠送鼓励金提前分佣");
							}
							/***************************提现分佣end********************************************/
						}
					}else{
						//判断商家是否抵扣鼓励金
						if($goods_info['dk_price']>0){
							//计算抵扣鼓励金 = 商品抵扣金额 * 商品数量
							$score_change = bcmul($goods_info['dk_price'] , $paylog['goods_num'], 4);
							//更新商家，用户鼓励金变化情况(便于统计)
							db("log_paylog")
							->where('order_sn', $out_trade_no)
							->update(['score'=>$score_change,'member_score' =>-$score_change,'agent_score'=>$score_change,'payment_type'=>2]);
							//减用户鼓励金
							db('info_user')->where('id', $user_info['id'])->setDec('score', $score_change);
							//用户鼓励金记录
							$this->user_score_log($paylog,5,$score_change,2,"在商家".$agent_info['mobile']."消费,抵扣鼓励金:".$score_change);
							//加商家鼓励金
							db('info_agent')->where('id', $agent_info['id'])->setInc('score', $score_change);
							//商家鼓励金记录
							$this->agent_score_log($paylog,3,$score_change,1,"收取用户".$user_info['mobile']."鼓励金抵扣:".$score_change);
							//鼓励金费率
							$score_fee = 0.05;
							//计算鼓励金手续费
							$poundage = bcmul($score_change, $score_fee, 4);
							if(bccomp(floatval($poundage), floatval(0.1), 2) >= 0){
								//减去商家鼓励金手续费
								db('info_agent')->where('id', $agent_info['id'])->setDec('score', $poundage);
								//减去商家鼓励金手续费日志
								$this->agent_score_log($paylog,$order_type,$poundage,2,"鼓励金抵扣手续费:".$poundage);
							}
						}
					}
				}
				//新增用户金额变化日志
				$this->user_money_log($paylog,5,$paylog['balance'],2,"向商家二维码付款:".$paylog['balance']);
				//增加商家余额
				//db('info_agent')->where('id', $agent_info['id'])->setInc('balance', $paylog['balance']);
				//商家余额变化日志 
				$this->agent_money_log($paylog,3,$paylog['balance'],1,"用户扫码付款:".$paylog['balance']);
				
				//最低费率
				$rate = 0.0025;
				//商家费率
				$agent_rate = bcdiv($agent_info['cb_huilu'], 100, 4);
				//市代费率佣金 = 商家费率-最低费率
				$city_rate = bcsub($agent_rate, $rate, 4);
				//市代费率佣金>=0.0001 给市代分佣
				if($agent_info['agency_id2'] && $agent_info['agency_id2']>0 && bccomp($city_rate,0.0001, 4)>=0){
					//计算市代佣金 = 商家收款金额 * 市代费率佣金
					$city_rate_commission = bcmul($paylog['balance'], $city_rate, 4);
					//新增市代佣金
					db('info_oem')->where('id', $agent_info['agency_id2'])->setInc('commission', $city_rate_commission);
					//市代佣金记录
					$this->oem_commission_log($paylog,7,$city_rate_commission,1,3,"商家".$agent_info['mobile']."费率佣金");
				}
	
			/********************************广告费分佣start***************************************/
				//判断付款用户是否是自己的线下会员
				$cut_shou_user = db('info_user')->where('id',$agent_info['user_id'])->find();
				//开启云商圈
				if($user_info['level'] != $cut_shou_user['id']){
					//单客次广告费
					$ad_one_money = 0.2;
					//联盟主广告佣金比例
					$union_main_scale = 0.2;
					//付款用户上级广告佣金比例
					$user_up_scale = 0.3;
					//计算联盟主广告佣金20%,
					$union_main_adsmoney = bcmul($ad_one_money, $union_main_scale, 2);
					//计算付款用户上级广告佣金30%
					$user_up_adsmoney = bcmul($ad_one_money, $user_up_scale, 2);
					//查询商家已加入联盟 union_member
	    			$where['b.is_state'] = 1;	//联盟为正常状态
	    			$where['a.agent_id'] = $agent_info['id'];
	    			$where['a.state'] = 1;
	    	 
			    	$list = db('union_member')
							->alias('a')
							->field('a.u_id as union_user_id ')
							->join('union_agent b','a.union_id = b.id')
							->where($where)
							->order('a.add_time desc')
							->select();
							
					//检查商家是否是联盟成员
					if(count($list)>0){
						//计算商家本次需支付联盟主广告费
						$union_main_all_adsmoney = bcmul(count($list), $union_main_adsmoney, 2);
						//计算商家本次一共需支付广告费
						$total_adsmoney = bcadd($union_main_all_adsmoney, $user_up_adsmoney, 2);
						//当前店铺广告费余额大于本次广告费，广告费大于等于0.1元
						if(bccomp($agent_info['ad_money'], $total_adsmoney, 2)>=0){
							//$user_info $agent_info $cut_shou_user
							//减去商家广告费
							db('info_agent')->where('id', $agent_info['id'])->setDec('ad_money', $total_adsmoney);
							//商家广告费日志 
							$this->agent_money_log($paylog,8,$total_adsmoney,2,"支付广告费");
							//增加付款用户上级佣金
							db('info_user')->where('id', $user_info['level'])->setInc('commission', $user_up_adsmoney);
							//用户上级分佣日志
							$this->user_commission_log($paylog,3,$user_up_adsmoney,1,"收取推荐会员:".$user_info['mobile']."广告佣金");
							//检查用户是否加入联盟
							if(count($list)>0){
								//向联盟主支付广告费
								for($i=0;$i<count($list);$i++){
									//增加付款用户上级佣金
									db('info_user')->where('id', $list[$i]['union_user_id'])->setInc('commission', $union_main_adsmoney);
									//用户上级分佣日志
									$this->user_commission_log($paylog,3,$union_main_adsmoney,1,"联盟成员:".$user_info['mobile']."广告佣金");
								}
							}else{
								//增加OEM广告费佣金
								db('info_oem')->where('id', $agent_info['oem_id'])->setInc('commission', $union_main_adsmoney);
								//更新OEM佣金日志
								$this->oem_commission_log($paylog,3,$union_main_adsmoney,1,1,"商家".$agent_info['mobile']."鼓励金佣金");
							}
						}else{
							//商家广告费不足，关闭商家云商圈
							db('info_agent')->where('id', $agent_info['id'])->update(['is_union' => 2]);
						}
					}
				}
	
			/********************************广告费分佣end***************************************/
				// 获取短信模板
				$template = db('sms_template') -> find(4);
				$msg['name'] = $price;
				//检查是否分店
				if($paylog['chain_id']>0){
					//获取收银员信息
					$employee_info = db('agent_clerk')->where('id',$paylog['clerk_id'])->find();
					//发送短信，提醒分店负责人
					$msg = send_sms($employee_info['mobile'], $template['tid'], json_encode($msg), $template['sign_name']);
				}else{
					//发送短信，提醒商家
					$msg = send_sms($agent_info['tel'], $template['tid'], json_encode($msg), $template['sign_name']);
				}
				//获取当前商品信息
				$goods_info = Db::table('agent_goods')->where('id', $paylog['goods_id'])->find();
				//计算剩余库存
				$goods_stock = $goods_info['stock'] - $paylog['goods_num'];
				//计算累计销量
				$goods_sales = $goods_info['sales'] + $paylog['goods_num'];
				//判断是否抵扣
				if(($paylog['balance'] * 100) == ($paylog['origin_price'] * 100)){
					//累计销售金额
					$total_money 	= bcadd($goods_info['total_money'], bcmul($goods_info['original_price'], $paylog['goods_num']), 2);
					//累计赠送积分
					$total_give_score = bcadd($goods_info['total_give_score'], bcmul($goods_info['zs_price'], $paylog['goods_num']), 2);
					//累计抵扣积分	
					$total_need_score = $goods_info['total_need_score'];
				}else{
					//累计销售金额
					$total_money 	= bcadd($goods_info['total_money'], bcmul($goods_info['sale_price'], $paylog['goods_num']), 2);
					//累计赠送积分
					$total_give_score = $goods_info['total_give_score'];
					//累计抵扣积分	
					$total_need_score = bcadd($goods_info['total_need_score'], bcmul($goods_info['dk_price'], $paylog['goods_num']), 2);
				}
				
				//减库存
				Db::table('agent_goods')
				->where('id', $goods_info['id'])
				->update(['stock' => $goods_stock,
							'sales'=>$goods_sales,
							'total_money'=>$total_money,
							'total_give_score'=>$total_give_score,
							'total_need_score'=> $total_need_score
							]);
				//提交
				Db::commit();
				//修改订单状态
				$this->add_corepaylog($paylog);
			}catch ( Exception $e ) {
			    dump($e->getMessage());
			    //回滚
			    Db::rollback();
			    exit;
			}
		}
	}
	
//付款成功
function add_corepaylog($paylog){
	//更新付款状态
	db('log_paylog')->where('id', $paylog['id'])->update(['state' => 2]);
	$corepaylog=new CorePayLog();
	unset($paylog['id']);
	$res=$corepaylog->allowField(true)->save($paylog);
	return $res;
}	
	
	
	
	/**
	 * 用户资金变化日志
	 * 
	 * $paylog 支付日志
	 * $order_type 订单类型
	 * $operator 加减符号
	 * $message	资金变化详情
	 */
	function user_money_log($paylog,$order_type,$balance,$operator,$message){
		$user_info = db('info_user')->where('id',$paylog['uid'])->find();
		
		$salesman_info = db('info_salesman')->where('id',$user_info['salesman_id'])->find();
		
		//累计消费次数
		$total_nums = $user_info['total_nums']+1;
		//累计消费金额
		$total_balance = bcadd($user_info['total_balance'], $balance, 2);
		
		Db::table('info_user')->where('id', $user_info['id'])
		->update(['total_balance' => $total_balance,'total_nums'=>$total_nums]);
		
		$data = array(
			'u_id'		 	=> $user_info['id'],
			'order_sn' 	 	=> $paylog['order_sn'],
			'order_type' 	=> $order_type,
			'balance' 	 	=> $balance,
			'operator' 	 	=> $operator,
			'message' 	 	=> $message,
		    'pay_type' 	 	=> $paylog['pay_type'],
		    'oem_id' 	 	=> $user_info['oem_id'],
		    "agency_id"	 	=> $salesman_info['agency_id1'],
		    'city_id' 	 	=> $salesman_info['agency_id2'],
		    "add_time"	 	=> now_time()
		);
		$result = db('log_user_charge')->insert($data);
		
		$user_relevance_agent = db('user_relevance_agent')
								->where('user_id',$paylog['uid'])
								->where('agent_id',$paylog['agent_id'])
								->find();
		if($user_relevance_agent){
			dump('1111');
			$relevance['total_money'] = bcadd($user_relevance_agent['total_money'], $paylog['balance'], 2);
			$relevance['total_num'] = $user_relevance_agent['total_num']+1;
			Db::table('user_relevance_agent')
					->where('id', $user_relevance_agent['id'])
					->update($relevance);
		}else{
			$relevance['user_id'] = $paylog['uid'];
			$relevance['agent_id'] = $paylog['agent_id'];
			$relevance['total_money'] = $paylog['balance'];
			$relevance['total_num'] = 1;
			
			Db::table('user_relevance_agent')->insert($relevance);
		}
		return $result;
	}
	
	/**
	 * 用户鼓励金变化日志
	 * 
	 * $paylog 支付日志
	 * $order_type 订单类型
	 * $balance	金额变化值
	 * $operator 加减符号
	 * $message	变化详情
	 */
	
	function user_score_log($paylog,$order_type,$balance,$operator,$message){
		
		$user_info = db('info_user')->where('id',$paylog['uid'])->find();
		
		$salesman_info = db('info_salesman')->where('id',$user_info['salesman_id'])->find();
		
		$data = array(
			'u_id'		 => $user_info['id'],
			'order_sn' 	 => $paylog['order_sn'],
			'order_type' => $order_type,
			'balance' 	 => $balance,
			'operator' 	 => $operator,
			'message' 	 => $message,
		    'pay_type' 	 => $paylog['pay_type'],
		    'oem_id' 	 => $user_info['oem_id'],
		    "agency_id"	 => $salesman_info['agency_id1'],
		    'city_id' 	 => $salesman_info['agency_id2'],
		    "add_time"	 => now_time()
		);
		return db('log_user_score')->insert($data);
		
	}
	
	/**
	 * 用户佣金变化日志
	 * 
	 * $paylog 支付日志
	 * $order_type 订单类型
	 * $balance 变化金额
	 * $operator 加减符号
	 */
	function user_commission_log($paylog,$order_type,$balance,$operator,$message){
		
		$user_info = db('info_user')->where('id',$paylog['uid'])->find();
		
		if($paylog['order_type'] == 3){
			$u_id = $user_info['level'];
		}else{
			$u_id = $user_info['id'];
		}
		
		$salesman_info = db('info_salesman')->where('id',$user_info['salesman_id'])->find();
		$data = array(
			'u_id'		 => $u_id,
			'order_sn' 	 => $paylog['order_sn'],
			'order_type' => $order_type,
			'balance' 	 => $balance,
			'operator' 	 => $operator,
			'message' 	 => $message,
		    'pay_type' 	 => $paylog['pay_type'],
		    'oem_id' 	 => $user_info['oem_id'],
		    "agency_id"	 => $salesman_info['agency_id1'],
		    'city_id' 	 => $salesman_info['agency_id2'],
		    "add_time"	 => now_time()
		);
		return db('log_user_commission')->insert($data);
	}
	
	
	/**
	 * 商家资金变化日志
	 * 
	 * $paylog 支付日志
	 * $order_type 订单类型
	 * $message	资金变化详情
	 * $operator 加减符号
	 */
	function agent_money_log($paylog,$order_type,$balance,$operator,$message){
		
		$agent_info = db('info_agent')->where('id',$paylog['agent_id'])->find();
		
		$data = array(
			'agent_id'		=> $agent_info['id'],
			'chain_id' 	 	=> $paylog['chain_id'],
			'clerk_id' 		=> $paylog['clerk_id'],
			'order_sn' 	 	=> $paylog['order_sn'],
			'order_type' 	=> $order_type,
			'message' 	 	=> $message,
		    'balance' 	 	=> $balance,
		    'operator' 	 	=> $operator,
		    "store_balance"	=> $agent_info['balance'],
		    'pay_type' 	 	=> $paylog['pay_type'],
		    'status_tinyint'=> 1,
		    'oem_id' 	 	=> $agent_info['oem_id'],
		    'agency_id' 	=> $agent_info['agency_id1'],
		    'city_id' 	 	=> $agent_info['agency_id2'],
		    'salesman_id' 	=> $agent_info['salesman_id'],
		    "add_time"	 	=> now_time()
		);
		return db('log_agent_charge')->insert($data);
	}
	
	/**
	 * 商家鼓励金变化日志
	 * 
	 * $paylog 支付日志
	 * $order_type 订单类型
	 * $operator 加减符号
	 */
	function agent_score_log($paylog,$order_type,$balance,$operator,$message){
		$agent_info = db('info_agent')->where('id',$paylog['agent_id'])->find();
		
		$data = array(
			'agent_id'		=> $agent_info['id'],
			'chain_id' 	 	=> $paylog['chain_id'],
			'clerk_id' 		=> $paylog['clerk_id'],
			'order_sn' 	 	=> $paylog['order_sn'],
			'order_type' 	=> $order_type,
			'message' 	 	=> $message,
		    'balance' 	 	=> $balance,
		    'operator' 	 	=> $operator,
		    'pay_type' 	 	=> $paylog['pay_type'],
		    'status_tinyint'=> 1,
		    'oem_id' 	 	=> $agent_info['oem_id'],
		    'agency_id' 	=> $agent_info['agency_id1'],
		    'city_id' 	 	=> $agent_info['agency_id2'],
		    "add_time"	 	=> now_time()
		);
		return db('log_agent_score')->insert($data);
	}
	
	/**
	 * 商家提前分佣记录
	 */
	function ahead_agent_score_log($paylog,$balance,$message){
		$agent_info = db('info_agent')->where('id',$paylog['agent_id'])->find();
		
		$data = array(
			'agent_id'		=> $agent_info['id'],
			'chain_id' 	 	=> 0,
			'clerk_id' 		=> 0,
			'order_sn' 	 	=> $paylog['order_sn'],
			'order_type' 	=> 8,
			'message' 	 	=> $message,
		    'balance' 	 	=> $balance,
		    'operator' 	 	=> 0,
		    'pay_type' 	 	=> $paylog['pay_type'],
		    'status_tinyint'=> 0,
		    'oem_id' 	 	=> $agent_info['oem_id'],
		    'agency_id' 	=> $agent_info['agency_id1'],
		    'city_id' 	 	=> $agent_info['agency_id2'],
		    "add_time"	 	=> now_time()
		);
		return db('log_agent_score')->insert($data);
	}
	/**
	 * OEM资金变化日志
	 * 
	 * $paylog 支付日志
	 * $order_type 订单类型
	 * $balance 变化金额
	 * $operator 加减符号
	 */
	function oem_money_log($paylog,$order_type,$balance,$operator,$type,$message){
		
		$agent_info = db('info_agent')->where('id',$paylog['agent_id'])->find();
		
		$data = array(
			'o_id'			=> $agent_info['oem_id'],
			'order_sn' 	 	=> $paylog['order_sn'],
			'order_type' 	=> $order_type,
			'message' 	 	=> $message,
		    'balance' 	 	=> $balance,
		    'operator' 	 	=> $operator,
		    'user_type' 	=> $type,
		    'pay_type' 	 	=> $paylog['pay_type'],
		    "add_time"	 	=> now_time()
		);
		
		if($type==1){
			$data['oem_id'] = $agent_info['oem_id'];
		}else if($type==2){
			$data['oem_id'] = $agent_info['oem_id'];
			$data['agency_id'] = $agent_info['agency_id1'];
			
		}else{
			$data['oem_id'] = $agent_info['oem_id'];
			$data['agency_id'] = $agent_info['agency_id1'];
			$data['city_id'] = $agent_info['agency_id2'];
		}
		
		return db('log_oem_charge')->insert($data);
	}
	
	/**
	 * OEM佣金变化日志
	 * 
	 * $paylog 支付日志
	 * $order_type 订单类型
	 * $balance 变化金额
	 * $operator 加减符号
	 */
	function oem_commission_log($paylog,$order_type,$balance,$operator,$type,$message){
		
		$agent_info = db('info_agent')->where('id',$paylog['agent_id'])->find();
		
		$data = array(
			'o_id'			=> $agent_info['oem_id'],
			'order_sn' 	 	=> $paylog['order_sn'],
			'order_type' 	=> $order_type,
			'message' 	 	=> $message,
		    'balance' 	 	=> $balance,
		    'operator' 	 	=> $operator,
		    'user_type' 	=> $type,
		    'pay_type' 	 	=> $paylog['pay_type'],
		    "add_time"	 	=> now_time()
		);
		
		if($type==1){
			$data['oem_id'] = $agent_info['oem_id'];
		}else if($type==2){
			$data['oem_id'] = $agent_info['oem_id'];
			$data['agency_id'] = $agent_info['agency_id1'];
		}else{
			$data['oem_id'] = $agent_info['oem_id'];
			$data['agency_id'] = $agent_info['agency_id1'];
			$data['city_id'] = $agent_info['agency_id2'];
		}
		
		return db('log_oem_commission')->insert($data);
	}
    
}